package com.igoal.entity.factory;

import java.util.Calendar;
import java.util.Date;

import com.igoal.campaign.xmas2010.entity.Xmas2010;
import com.igoal.dto.UserDTO;
import com.igoal.entity.ActionTypeEnum;
import com.igoal.entity.Audit;
import com.igoal.entity.Category;
import com.igoal.entity.Comment;
import com.igoal.entity.Company;
import com.igoal.entity.ComponentEnum;
import com.igoal.entity.DataStatus;
import com.igoal.entity.Goal;
import com.igoal.entity.GoalCategory;
import com.igoal.entity.GoalStatusEnum;
import com.igoal.entity.Hobby;
import com.igoal.entity.Job;
import com.igoal.entity.Message;
import com.igoal.entity.News;
import com.igoal.entity.ProfileComplete;
import com.igoal.entity.Role;
import com.igoal.entity.School;
import com.igoal.entity.Tag;
import com.igoal.entity.User;
import com.igoal.entity.UserActivity;
import com.igoal.entity.UserCategory;
import com.igoal.entity.UserCheer;
import com.igoal.entity.UserCompany;
import com.igoal.entity.UserFriend;
import com.igoal.entity.UserGoal;
import com.igoal.entity.UserGoalDone;
import com.igoal.entity.UserGoalEntry;
import com.igoal.entity.UserGoalPhase;
import com.igoal.entity.UserGoalTask;
import com.igoal.entity.UserInfo;
import com.igoal.entity.UserLike;
import com.igoal.entity.UserRole;
import com.igoal.entity.UserSchool;
import com.igoal.entity.UserStatus;
import com.igoal.entity.UserTag;
import com.igoal.entity.UserThought;
import com.igoal.entity.UserThumb;
import com.igoal.entity.WallUpdate;
import com.igoal.utils.EntityUtils;
import com.igoal.utils.StringUtils;
import com.igoal.web.Constants;

public class EntityFactory {
	
	public static final User createRegisterUser(UserDTO userDTO, String encryptedPassword, String activationCode){
		if(userDTO == null)
			return null;
		
		User user = new User();		
		userDTO.copyAttributesTo(user);
		user.setUsername(user.getUsername().toLowerCase());
		user.setPassword(encryptedPassword);
		user.setActivationCode(activationCode);
		user.setNumberFollowed(0);
		user.setNumberFollows(0);
		user.setNumberFriends(0);
		user.setNumberGoals(0);
		user.setEnabled(false);
		user.setSubscriptionType(Constants.USER_SUBSCRIPTION_MONTHLY);
		
		initUserInfo(user);
		
		setAuditInformation(user);
		return user;
	}
	
	public static final void initUserInfo(User user){
		UserInfo userInfo = user.getUserInfo();
		int day = user.getDay();
		int month = user.getMonth();
		int year = user.getYear();

		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day);
		Date dob = cal.getTime();

		if (userInfo == null)
			userInfo = new UserInfo();
		userInfo.setBirthday(dob);

		setAuditInformation(userInfo);
		
		userInfo.setUser(user);
		user.setUserInfo(userInfo);
	}
	
	public static final UserFriend createUserFriend(User person, User friend) {
		UserFriend userFriend = new UserFriend();
		userFriend.setFrom(person);
		userFriend.setTo(friend);
		userFriend.setAccepted(false);
		setAuditInformation(userFriend);
		
		return userFriend;
	}

	public static final Goal createGoal() {
		Goal goal = new Goal();
		goal.setUserCount(1);
		goal.setDoneCount(0);
		setAuditInformation(goal);

		return goal;
	}

	public static final UserGoal createUserGoal(User person, Goal goal) {
		UserGoal userGoal = new UserGoal();
		userGoal.setPercent(0);
		userGoal.setGoal(goal);
		userGoal.setUser(person);
		userGoal.setTaskCount(0);
		userGoal.setGoalStatus(GoalStatusEnum.DOING);
		setAuditInformation(userGoal);

		return userGoal;
	}

	public static final UserGoalEntry createUserGoalEntry(UserGoal userGoal) {
		UserGoalEntry entry = new UserGoalEntry();
		entry.setUserGoal(userGoal);
		entry.setImgURL(null);
		entry.setBlock(false);
		setAuditInformation(entry);

		return entry;
	}

	public static final UserGoalDone createUserGoalDone(UserGoal userGoal) {
		UserGoalDone entry = new UserGoalDone();
		entry.setUserGoal(userGoal);
		entry.setBlock(false);
		setAuditInformation(entry);
		entry.setCheerCount(0);
		entry.setCommentCount(0);

		return entry;
	}

	public static final UserStatus createUserStatus(User user) {
		UserStatus userStatus = new UserStatus();
		userStatus.setUser(user);
		setAuditInformation(userStatus);

		return userStatus;
	}

	public static final Message createMessage(User from, User to) {
		Message message = new Message();
		message.setFrom(from);
		message.setTo(to);
		message.setCreateDate(new Date());
		message.setStatus(DataStatus.USING);
		message.setMessageStatus(true);
		return message;
	}

	public static final UserLike createUserLike(User user, ComponentEnum component, Long componentId) {
		UserLike userLike = new UserLike();
		userLike.setUser(user);
		userLike.setComponent(component);
		userLike.setComponentId(componentId);
		setAuditInformation(userLike);

		return userLike;
	}

	public static final UserCheer createUserCheer(User user, ComponentEnum component, Long componentId) {
		UserCheer userCheer = new UserCheer();
		userCheer.setUser(user);
		userCheer.setComponent(component);
		userCheer.setComponentId(componentId);
		setAuditInformation(userCheer);
		
		return userCheer;
	}

	public static final Category createCategory(String category) {
		Category entity = new Category();
		entity.setCategory(category);
		setAuditInformation(entity);

		return entity;
	}

	public static final GoalCategory createGoalCategory(Goal goal, Category category) {
		GoalCategory goalCategory = new GoalCategory();
		goalCategory.setGoal(goal);
		goalCategory.setCategory(category);
		setAuditInformation(goalCategory);

		return goalCategory;
	}

	public static final Hobby createHobby(String name, String category) {
		Hobby h = new Hobby();
		h.setName(name);
		h.setCategory(category);
		setAuditInformation(h);
		return h;
	}

	public static final ProfileComplete createProfileComplete(User user) {
		ProfileComplete profileComplete = new ProfileComplete();
		profileComplete.setId(user.getId());
		profileComplete.setBasicInfo(false);
		profileComplete.setAvatar(false);
		profileComplete.setHobby(false);
		profileComplete.setAddGoal(false);
		profileComplete.setCreateDate(new Date());
		profileComplete.setUpdateDate(new Date());
		profileComplete.setStatus(DataStatus.USING);
		return profileComplete;
	}
	
	public static final UserCategory createUserCategory(User user, Category category){
		UserCategory userCategory = new UserCategory();
		userCategory.setUser(user);
		userCategory.setCategory(category);
		setAuditInformation(userCategory);
		
		return userCategory;
	}
	
	public static final Job createJob(String name) {
		Job job = new Job();
		job.setJobName(name);
		setAuditInformation(job);
		return job;
	}

	public static final School createSchool(String name) {
		School school = new School();
		school.setName(name);
		setAuditInformation(school);
		return school;
	}
	
	public static final void setAuditInformation(Audit audit){
//		audit.setCreateDate(new Date());
//		audit.setUpdateDate(new Date());
//		audit.setStatus(DataStatus.USING);
		EntityUtils.createNewAuditInformation(audit);
	}
	
	public static final UserRole createUserRole(User user, Role role){
		UserRole userRole = new UserRole();
		userRole.setRole(role);
		userRole.setUser(user);
		setAuditInformation(userRole);
		
		return userRole;
		
	}
	
	public static final UserThumb createUserThumb(User user, ComponentEnum component, Long componentId) {
		UserThumb userThumb = new UserThumb();
		userThumb.setUser(user);
		userThumb.setComponent(component);
		userThumb.setComponentId(componentId);
		setAuditInformation(userThumb);

		return userThumb;
	}
	
	public static final UserActivity createUserActivity(User componentOwner, User actionOwner, ActionTypeEnum actionType,
			ComponentEnum componentType, Long componentId) {
		UserActivity action = new UserActivity();
		action.setComponentOwner(componentOwner);
		action.setActionOwner(actionOwner);
		action.setActionType(actionType);
		action.setComponent(componentType);
		action.setComponentId(componentId);
		action.setRead(false);
		setAuditInformation(action);
		return action;
	}

	public static final WallUpdate createWallUpdate(User componentOwner, ComponentEnum componentType, Long componentId) {
		WallUpdate update = new WallUpdate();
		update.setComponentOwner(componentOwner);
		update.setComponent(componentType);
		update.setComponentId(componentId);
		setAuditInformation(update);
		return update;
	}
	
	public static final UserGoalTask createUserGoalTask(UserGoalPhase phase) {
		UserGoalTask task = new UserGoalTask();
		task.setUserGoalPhase(phase);				
		task.setPercentMeasure(phase.getPercentMeasure());
		task.setDeadLine(phase.getDeadLine());
		task.setTaskName(phase.getPhaseName());
		task.setFinished(Boolean.FALSE);
		task.setIndex(1);		
		setAuditInformation(task);

		return task;
	}
	
	public static final Comment createComment(User user){
		Comment comment = new Comment();
		comment.setUser(user);
		setAuditInformation(comment);
		
		return comment;
	}
	
	public static final UserThought createWallPost(User postOwner, User wallOwner) {
		UserThought wallPost = new UserThought();
		wallPost.setPostOwner(postOwner);
		wallPost.setWallOwner(wallOwner);
		setAuditInformation(wallPost);
		return wallPost;
	}
	
	public static final News createNews(User user){
		News news = new News();
		news.setUser(user);
		news.setCommentCount(0);
		setAuditInformation(news);
		return news;
	}
	
	public static final Tag createTag(String tagName){
		Tag tag = new Tag();
		tag.setTagName(tagName);
		setAuditInformation(tag);
		return tag;
	}
	
	public static final UserTag createUserTag(Tag tag, User user){
		UserTag userTag = new UserTag();
		userTag.setUser(user);
		userTag.setTag(tag);
		setAuditInformation(userTag);
		return userTag;
	}
	
	public static final UserGoalPhase createUserGoalPhase(UserGoal userGoal){
		UserGoalPhase phase = new UserGoalPhase();
		phase.setUserGoal(userGoal);
		phase.setPercentMeasure(0);
		phase.setTaskCount(0);
		phase.setIndex(1);
		phase.setRemainingTaks(0);
		phase.setFailed(Boolean.FALSE);
		phase.setFinished(Boolean.FALSE);
		setAuditInformation(phase);
		return phase;
	}
	
	public static final UserSchool createUserSchool(User user, School school){
		UserSchool userSchool = new UserSchool();
		userSchool.setUser(user);
		userSchool.setSchool(school);
		setAuditInformation(userSchool);
		return userSchool;
	}
	
	public static final UserCompany createUserCompany(User user, Company company){
		UserCompany userCompany = new UserCompany();
		userCompany.setUser(user);
		userCompany.setCompany(company);
		setAuditInformation(userCompany);
		return userCompany;
	}
	
	public static final Company createCompany(String name){
		Company company = new Company();
		company.setName(name);
		setAuditInformation(company);
		return company;
	}
	
	public static final Xmas2010 createXmasWish(User user, String wish, String image, String video) {
		Xmas2010 xmasWish = new Xmas2010();
		xmasWish.setOwner(user);
		if (StringUtils.isNotBlank(image)) {
			xmasWish.setImgUrl(image);
		}
		if (StringUtils.isNotBlank(video)) {
			xmasWish.setVideoUrl(video);
		}
		xmasWish.setCreateDate(new Date());
		return xmasWish;
	}
	
//	@Deprecated
//	public static final UserActivity createUserActivity(User user, UserNotificationLog log){
//		UserActivity userActivity = new UserActivity();
//		userActivity.setLog(log);
//		userActivity.setUser(user);
//		setAuditInformation(userActivity);
//		
//		return userActivity;
//	}
	
}
